package com.ami12.shopping.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * Created by zhangzejun .
 * time: 2016/12/8  14:18
 */
@Controller
@RequestMapping("/test")
public class TestController {
    private static final String SUCCESS = "success";

    @RequestMapping("/index")
    public String index(){
        return "upload";
    }

    /**
     * MultipartFile是个接口
     * 上传图片到本地
     * @param desc
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("/upload")
    public String testUpload(@RequestParam("desc") String desc, @RequestParam("file")MultipartFile file) throws IOException {
        System.out.println("desc :"+desc);
        System.out.println("文件原始名 ："+file.getOriginalFilename());
        System.out.println("文件输入流 ："+file.getInputStream());
        if(file != null){
            String file_path = "G:\\";
            String origin_name = file.getOriginalFilename();
            String new_name = UUID.randomUUID()+ origin_name.substring(origin_name.lastIndexOf("."));
            //新图片
            File newFile = new File(file_path+new_name);
            //将内存中的数据写入磁盘
            file.transferTo(newFile);
        }
        return SUCCESS;
    }
}


















